<?php

use \Mapper\WithdrawModel as WithdrawMapper;
use \Mapper\MemberModel as MemberMapper;

class WithdrawController extends \Base\AbstractController {

    public function listAction() {
        $request = $this->getRequest();
        $status  = (int)$request->get('status');
        $where   = [];

        if($status > 0) {
            $where['status'] = $status;
        }

        $paginator = new \Lib\Paginator('withdraw');
        $paginator->where($where);

        $this->assign('page', $paginator->getPage());
        $this->assign('count', $paginator->count());
        $this->assign('perPage', $paginator->getPerpage());
        $this->assign('lists', $paginator->lists());
    }

    public function editAction() {
        $request = $this->getRequest();
        $id      = (int)$request->get('id');
        $status  = (int)$request->get('status');
        $remark  = (string)trim($request->get('remark'));
        $mapper  = WithdrawMapper::getInstance();
        $model   = $mapper->findById($id);

        if(!$model instanceof \WithdrawModel) {
            return false;
        }

        if($request->isPost()) {
            if($model->getStatus() === \Lib\Consts::W_TYPE_SUCCESS ||
                $model->getStatus() === \Lib\Consts::W_TYPE_FAILURE
            ) {
                return \Response::errmsg('不能重复提现!');
            }

            $model->setStatus($status);
            $model->setUpdate_time(time());
            $model->setRemark($remark);
            $errMsg = $this->validate($model);

            if($errMsg !== false) {
                return \Response::errmsg($errMsg);
            }

            $memberMapper = MemberMapper::getInstance();
            $memberModel  = $memberMapper->findById($model->getUid());

            if(!$memberModel instanceof \MemberModel) {
                return \Response::errmsg('uid有误');
            }

            if($status === \Lib\Consts::W_TYPE_SUCCESS) {
                $mapper->begin();

                try {
                    $rows = $mapper->update($model, ['id' => $id]);
                } catch(\Exception $e) {
                    $rows = 0;
                }

                if($rows <= 0) {
                    $mapper->rollback();
                    return \Response::errmsg('变更状态失败!');
                }

                $return = \Lib\Pay\Wechat::getInstance()->pay($memberModel->getOpen_id(), 100);

                if($return !== true) {
                    $mapper->rollback();
                    return \Response::errmsg($return);
                }

                $mapper->commit();
                return \Response::msg('提现成功');
            }

            $return = \Lib\Finance::getInstance()->flow($memberModel->getId(), \Lib\Consts::F_WITHDRAW_FAILED,
                $model->getSum(), function () use ($model, $mapper) {
                    try {
                        $rows = $mapper->update($model, ['id' => $model->getId()]);
                    } catch(\Exception $e) {
                        $rows = 0;
                    }

                    return $rows > 0 ? true : false;
                });

            if($return) {
                return \Response::msg();
            } else {
                return \Response::errmsg('拒绝提现失败!');
            }
        }

        $this->assign('model', $model);
    }

    private function validate(\WithdrawModel $model) {
        if(empty($model->getStatus())) {
            return '状态值不能为空';
        }

        if($model->getStatus() !== \Lib\Consts::W_TYPE_SUCCESS && $model->getStatus() !== \Lib\Consts::W_TYPE_FAILURE) {
            return '状态值有误';
        }

        return false;
    }

}
